import grammar.Design;
import grammar.InstantiationInput;
import grammar.Match;
import grammar.Matcher;
import grammar.Rule;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;

import test.ShapeAnneal;
import test.TestData;


public class ShapeAnnealGrammar {
	
	@SuppressWarnings("all")
		    private Design shapeGrammarMain() {
	        // build/read rules and initial condition
	    	
	    	//To build snake :
	        Design design = ShapeAnneal.buildInitialConfiguration();
	        Rule rule1 = ShapeAnneal.straight();
	        Rule rule2 = ShapeAnneal.counterClockwise();
	        Rule rule3 = ShapeAnneal.clockwise();
	    	

	        java.util.List<Rule> rules = new ArrayList<Rule>();
	        rules.add(rule1);
	        rules.add(rule2);
	        rules.add(rule3);

	        Random rand = new Random(System.currentTimeMillis());
	        
//	        build/read script
	        for (int j = 0; j<25; j++) {        
	            //where is rule 1 applicable
	            Rule rule = (Rule) chooseRandom(rules, rand);
	            System.out.println("rule number chosen = " + rules.indexOf(rule));
	            
	            java.util.List<Match> matches = Matcher.findRuleMatches(design, rule);
	            System.out.println("number of matches = " + matches.size());
	            
	            if (matches.isEmpty()) {
	                continue;
	            }
	            
	            //choose where to apply rule
	            Match match = matches.get(matches.size()-1);

//	          apply rule and instantiate needed parameters
	            InstantiationInput input = new InstantiationInput();
	            rule.apply(design, match, input);
	        }
	        design.writeVRML("design.wrl");
	        return design;
	    }

	    private Object chooseRandom(List<? extends Object> list, Random rand) {
	        int randInt = rand.nextInt(list.size()); 
	        return list.get(randInt);
	    }

	    //Quit the application.
	    protected void quit() {
	        System.exit(0);
	    }
	 
	    /**
	     * Create the GUI and show it.  For thread safety,
	     * this method should be invoked from the
	     * event-dispatching thread.
	     */
	    private static void createAndShowGUI() {
	        //Make sure we have nice window decorations.
	        JFrame.setDefaultLookAndFeelDecorated(true);
	 
	        //Create and set up the window.
	        GrammarGui frame = new GrammarGui();
	        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	 
	        //Display the window.
	        frame.setVisible(true);
	    }
	 
	    public static void main(String[] args) {
	        //Schedule a job for the event-dispatching thread:
	        //creating and showing this application's GUI.
	        javax.swing.SwingUtilities.invokeLater(new Runnable() {
	            public void run() {
	                createAndShowGUI();
	            }
	        });
	    }
	}


